package com.ezuikit.open;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.ezvizuikit.open.EZUIKit;
import com.ezvizuikit.open.EZUIPlayer;
import com.videogo.exception.BaseException;

import com.videogo.openapi.EZConstants.EZPTZCommand;
import com.videogo.openapi.EZConstants.EZPTZAction;
import com.videogo.openapi.EZConstants;

import com.videogo.openapi.EZOpenSDK;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.videogo.exception.InnerException;


public class PTZcontrol {

      
       public void control(String accessToken ,String DeviceSerial ,int CameraNo,int i) {
           switch (i) {
           
           case 1:
               ptzOption(DeviceSerial,CameraNo,EZPTZCommand.EZPTZCommandUp, EZPTZAction.EZPTZActionSTART);
               break;
           case 2:
                ptzOption(DeviceSerial,CameraNo,EZPTZCommand.EZPTZCommandDown, EZPTZAction.EZPTZActionSTART);
               break;
           case 3:
               ptzOption(DeviceSerial,CameraNo,EZPTZCommand.EZPTZCommandLeft, EZPTZAction.EZPTZActionSTART);
               break;
           case 4:
               ptzOption(DeviceSerial,CameraNo,EZPTZCommand.EZPTZCommandRight, EZPTZAction.EZPTZActionSTART);
               break;
           case 5:
               ptzOption(DeviceSerial,CameraNo,EZPTZCommand.EZPTZCommandUp, EZPTZAction.EZPTZActionSTOP);
               break;
           case 6:
                ptzOption(DeviceSerial,CameraNo,EZPTZCommand.EZPTZCommandDown, EZPTZAction.EZPTZActionSTOP);
               break;
           case 7:
               ptzOption(DeviceSerial,CameraNo,EZPTZCommand.EZPTZCommandLeft, EZPTZAction.EZPTZActionSTOP);
               break;
           case 8:
               ptzOption(DeviceSerial,CameraNo,EZPTZCommand.EZPTZCommandRight, EZPTZAction.EZPTZActionSTOP);
               break;

            case 9:
               ptzOption1(accessToken,CameraNo,DeviceSerial,i-1);
               break;
            case 10:
               ptzOption1(accessToken,CameraNo,DeviceSerial,i-1);
               break;
            case 11:
               ptzOption1(accessToken,CameraNo,DeviceSerial,i-1);
               break;
            case 12:
               ptzOption1(accessToken,CameraNo,DeviceSerial,i-1);
               break;            

                       
           default:
               break;
                   }
       }

    private void ptzOption(final String DeviceSerial,final int CameraNo,  final EZPTZCommand command, final EZPTZAction action) {
 
        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean ptz_result = false;
                try {
                   ptz_result = EZOpenSDK.getInstance().controlPTZ( DeviceSerial  , CameraNo, command,
                           action, 1);
                     } catch (BaseException e) {
                    e.printStackTrace();
                }
              }
        }).start();
    }

     public  void ptzOption1(final String accessToken,final int CameraNo,final String DeviceSerial,final int direction) {
       
        new Thread(new Runnable() {
            @Override
            public void run() {
                  HttpURLConnection conn = null;
                try {
                  final String url = "https://open.ys7.com/api/lapp/device/ptz/start?accessToken="+accessToken+"&deviceSerial="+DeviceSerial+"&channelNo="+CameraNo+"&direction="+direction+"&speed=1";
                  conn = (HttpURLConnection) new URL(url).openConnection(); 
                  conn.setRequestMethod("POST");
                  conn.setConnectTimeout(10000);
                  conn.setReadTimeout(10000);
                 
                  int responseCode = conn.getResponseCode();
                     } catch (Exception e) {
                    e.printStackTrace();
                }
              }
        }).start();
    }
     

        public static String readStream(InputStream is) {
             try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int len = 0;
            byte[] buffer = new byte[1024];

            while ((len = is.read(buffer)) != -1) {
              bos.write(buffer, 0, len);
            }

            is.close();
            return new String(bos.toByteArray());
          } catch (Exception e) {
            e.printStackTrace();
            return "解析出错";
          }
      }

      public  void saveCapturePictrue(String filePath, Bitmap bitmap) throws InnerException {
        if (TextUtils.isEmpty(filePath)){
            return;
        }
         File filepath = new File(filePath);
        File parent = filepath.getParentFile();
        if (parent == null || !parent.exists() || parent.isFile()) {
            parent.mkdirs();
        }
        FileOutputStream out = null;
        try {
            if (!TextUtils.isEmpty(filePath)) {
                out = new FileOutputStream(filepath);
                bitmap.compress(CompressFormat.JPEG, 100, out);
                out.flush();
                out.close();
                out = null;
            }


        } catch (FileNotFoundException e) {
           e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
